package com.heima.search.service.impl;

import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.search.pojo.ApAssociateWords;
import com.heima.search.service.AssociateWordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Lijx
 * @Date 2024-07-30 15:40
 */
@Service
public class ApAssociateWordsServiceImpl implements AssociateWordsService {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 关键字联想词加载列表
     * @param searchWords
     * @param pageSize
     * @return
     */
    @Override
    public ResponseResult searchWords(String searchWords, Integer pageSize) {
        if (searchWords == null || pageSize == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (pageSize > 20) {
            pageSize = 20;
        }
        //查询关键联想词列表
        List<ApAssociateWords> associateWords = mongoTemplate.find(Query.query(Criteria.where("associateWords").regex(".*?\\" + searchWords + ".*"))
                .limit(pageSize), ApAssociateWords.class);

        return ResponseResult.okResult(associateWords);
    }
}
